// pages/passwordLogin/passwordLogin.js
var app=getApp();
var util = require("../../utils/util");
var config=require("../../api/config");
var encryption=require("../../utils/md5");

Page({
  /*页面的初始数据*/
  data: {
    mobile:'',
    hide: false,
    password: '',
    agree: true
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {

  },
  // 勾选
  change_agree:function(){
    this.setData({agree: !this.data.agree})
  },
  show_toast:function(){
    util.page_show_toast('请先勾选同意家巴啦《用户协议》和《隐私政策》',2000);
  },
  // 协议跳转
  nav_go:function(e){
    var index=e.currentTarget.dataset.index,title=e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '../../mypage/webView/webView?title='+title+'&index='+index,
    })
  },
  // 手机号
  mobile_tap(e){
    var that = this;
    var val = e.detail.value;
    that.setData({mobile:val})
  },
  // 密码
  password_tap(e){
    var that = this;
    var val = e.detail.value;
    that.setData({password:val})
  },
  // 隐藏密码
  change_hide:function(){
    this.setData({hide:!this.data.hide})
  },
  // 忘记密码
  nav_forget:function(){
    wx.navigateTo({
      url: '../forgetPassword/forgetPassword',
    })
  },
  // 登录判断
  login_ver:function(){
    var that = this,mobile = that.data.mobile,password=that.data.password,agree=that.data.agree;
    var neg=(/^1[3456789]\d{9}$/);
    if(!neg.test(mobile)||!mobile){
      util.page_show_toast('手机号码输入有误',1500);
      return false;
    }
    if(!password||password.length<6){
      util.page_show_toast('密码输入有误',1500);
      return false;
    }
    if(!agree){
      that.show_toast();
      return false;
    }
    //
    that.login_by_password(mobile,password)
  },
  // 登录请求
  login_by_password:function(mobile,password){
    var that=this;
    var params={
      "phone":mobile,
      "password":encryption.md5(password)
    };
    config.login_by_password(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        app.globalData.login=true;
        app.globalData.change_dom=true;
        app.globalData.change_table=true;
        wx.setStorageSync('userinfo', info.userInfo)
        wx.setStorageSync('token', info.token)
        wx.setStorageSync('login_info', params)
        util.page_show_toast('登录成功',1500);
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }else{
        util.page_show_toast(res.data.message,2000);
      }
    })
  },
})